package P018_InstanceOf;

import org.junit.Test;

import java.util.Date;

/**
 * Desc:&emsp;&ensp; 避免instanceof非预期结果
 *
 * @author 李渊&emsp;liyuan7@yusys.com.cn
 * @version 1.0
 * @since V4.3&emsp;2022/2/24
 */
public class InstanceOf {
    @Test
    public void Test() {
        //String对象是否是Object的实例
        boolean bl = "Sting" instanceof Object;
        //String对象是否是String的实例
        boolean b2 = new String() instanceof String;
        //Object对象是否是String的实例
        boolean b3 = new Object() instanceof String;
        //拆箱类型是否是装箱类型的实例
//        boolean b4 = 'A' instanceof Character;
        //空对象是否是String的实例
        boolean b5 = null instanceof String;
        //类型转换后的空对象是否是String的实例
        boolean b6 = (String)null instanceof String;
        //Date对象是否是String的实例
//        boolean b7 = new Date() instanceof String;
        //在泛型类中判断String对象是否是Date的实例
        boolean b8 = new GenericClass<String>().isDateInstance("");
    }

    class GenericClass<T> {
        //判断是否是Date类型
        public boolean isDateInstance(T t) {
            return t instanceof Date;
        }
    }


}


